package com.zjw.spring_ai;

import jakarta.annotation.Resource;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("image")
public class ImageController {

    @Resource
    OpenAiImageModel openAiImageModel;

    @GetMapping(value = "/openAi")
    public Object image(@RequestParam("msg")String msg){
        ImageResponse response = openAiImageModel.call(new ImagePrompt(msg));
        String url = response.getResult().getOutput().getUrl();
        System.out.println(url);
        return url;
    }

    @RequestMapping("/openAi/option")
    private Object optionImage(@RequestParam(value = "msg") String msg) {
        ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg, OpenAiImageOptions.builder()
                .withQuality("hd") //高清图像
                .withN(1)  //生成1张图片
                .withHeight(1024) //生成的图片高度
                .withWidth(1024) //生成的图片宽度
                .build()));
        //把图片进行业务处理
        return imageResponse.getResult().getOutput().getUrl();
    }
}
